package com.myaihui.service.impl;

import com.myaihui.domain.Account;
import com.myaihui.domain.Market;
import com.myaihui.foundation.constant.SysUserConstant;
import com.myaihui.foundation.model.Pager;
import com.myaihui.foundation.query.MarketQuery;
import com.myaihui.repository.MarketRepository;
import com.myaihui.service.MarketService;
import com.myaihui.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * *******************************
 * Created by Tim.
 * form com.myaihui
 * on 2018/5/30
 * *******************************
 */
@Service
public class MarketServiceImpl implements MarketService {

    @Autowired
    private MarketRepository marketRepository;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public Pager<Market> getMarketPage(MarketQuery marketQuery) {
        Specification<Market> specification = new Specification<Market>() {
            @Override
            public Predicate toPredicate(Root<Market> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();

                if (StringUtils.isNotBlank(marketQuery.getName()))
                    predicates.add(criteriaBuilder.and(criteriaBuilder.like(root.get("name"), "%" + marketQuery.getName() + "%")));

                if (StringUtils.isNotBlank(marketQuery.getAddress()))
                    predicates.add(criteriaBuilder.and(criteriaBuilder.like(root.get("address"), "%" + marketQuery.getName() + "%")));

                query.where(predicates.toArray(new Predicate[predicates.size()]));
                query.orderBy(criteriaBuilder.desc(root.get("created")));
                return query.getRestriction();
            }
        };
        Pageable pageable = new PageRequest(marketQuery.getiDisplayStart(), marketQuery.getiDisplayLength());
        Page<Market> page = marketRepository.findAll(specification, pageable);

        return new Pager(page, marketQuery.getsEcho(), page.getContent());
    }

    @Override
    public Market add(Market market) {
        return marketRepository.save(market);
    }

    @Override
    public Market modify(Market market, Account account, String username, String password, String createdId) {
        if (StringUtils.isNotBlank(market.getId())) {
            Market oldMarket = marketRepository.findOne(market.getId());
            oldMarket.merge(market);
            oldMarket.setId(market.getId());
            oldMarket.setAccount(account);
            marketRepository.save(oldMarket);
            if (StringUtils.isNotBlank(password))
                sysUserService.modifyPassword(SysUserConstant.MARKET_ADMIN, market.getId(), password);
            return market;
        } else {
            market.setAccount(account);
            marketRepository.save(market);
            sysUserService.add(username, password, SysUserConstant.MARKET_ADMIN, market.getId(), createdId);
            return market;
        }
    }

    @Override
    public Market get(String id) {
        return marketRepository.findOne(id);
    }
}